package com.example.musicplayer

import android.media.MediaPlayer
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.net.toUri
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide

class MusicListAdapter(val musicList:List<music>,val onMusicClick:(music) -> Unit):RecyclerView.Adapter<MusicListAdapter.ViewHolder>(){

    inner class ViewHolder(view:View):RecyclerView.ViewHolder(view) {
        val MusicListImage:ImageView=view.findViewById(R.id.MusicListImage)
        val MusicListTitle:TextView=view.findViewById(R.id.MusicListTitle)
        val MusicListSinger:TextView=view.findViewById(R.id.MusicLIstSinger)
        fun bind(music:music) {
            itemView.setOnClickListener {
                onMusicClick(music)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view=LayoutInflater.from(parent.context).inflate(R.layout.music_list_item,parent,false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val musicListData=musicList[position]
        holder.bind(musicListData)
        holder.MusicListTitle.text=musicListData.title
        holder.MusicListSinger.text=musicListData.singer
        Glide.with(holder.itemView.context)
            .load(musicListData.coverImgUrl)
            .into(holder.MusicListImage)
    }

    override fun getItemCount(): Int {
        return musicList.size
    }

}